<?php
namespace app\controller;

use app\logic\PostLogic;
use app\model\post\PostModel;
use app\model\post\TypeModel;
use Exception;
use think\facade\View;

class Post
{
    // 注入用户逻辑服务
    protected $postLogic;
    function __construct(PostLogic $P)
    {
        $this->postLogic = $P;
    }

    // 主页（模板渲染入口）
    public function index($pid) {
        View::assign([
            "pid"   => $pid
        ]);
        return View::fetch();
    }
    // 列表页面
    public function list($type="all") {
        if ($type == "") $type = "all";
        View::assign([
            "type"  => $type
        ]);
        return View::fetch();
    }
    // 写新的文章
    public function edit_new() {
        return $this->edit("");
    }
    // 更新编辑文章
    public function edit($pid) {
        if ($pid == "") $pid = "0"; // pid == 0 说明是无文章，可以新建
        View::assign([
            "pid"   => $pid
        ]);
        return View::fetch("edit");
    }


    /*
        API : 获取文章个信息
        @url: /post/api/get/info
    */
    public function get_info() {
        $pid = input("get.pid");
        
        $V = new \app\validate\post\GetValidate();
        if(!$V->scene("info")->check([
            "pid"   => $pid
        ])) {
            return json(["msg" => $V->getError()]);
        }
        try {
            $P = $this->postLogic->getInfo($pid);
        } catch(Exception $e) {
            return json([
                "code"  => $e->getCode(),
                "msg"   => $e->getMessage()
            ]);
        }
        return json($P);
    }
    /*
        API : 上传信息
        url : /post/api/post/info

        // 应为上传内容是依赖于 pid 保存文件，所以前端使用同步方法
        // 在本接口获取到新建到数据库的 pid 后再上传内容
    */
    public function post_info($pid) {
        $V = new \app\validate\post\GetValidate();
        if(!$V->scene("info")->check([
            "pid"   => $pid
        ])) {
            return json(["msg" => $V->getError()]);
        }
        $data = [
            "title" => input("post.title"),
            "type"  => input("post.type")
        ];
        try {
            // 当前文章不存在，这是新建
            if ($pid == 0) {
                // 新建需要返回 pid ！
                $pid = $this->postLogic->createInfo($data);
            } else {
                // 更新
                $this->postLogic->updateInfo($pid,$data);
            }
        } catch(Exception $e) {
            return json([
                "code"  => $e->getCode(),
                "msg"   => $e->getMessage()
            ]);
        }
        return json([
            "code"  => 0,
            "msg"   => "更新成功",
            "pid"   => $pid
        ]);
    }

    /*
        API : 获取内容
        url : /post/api/get/content
    */
    public function get_content() {
        $pid = input("get.pid");
        
        $V = new \app\validate\post\GetValidate();
        if(!$V->scene("info")->check([
            "pid"   => $pid
        ])) {
            return json(["msg" => $V->getError()]);
        }

        try {
            $content = $this->postLogic->getContent($pid);
        } catch(Exception $e) {
            return json([
                "code"  => $e->getCode(),
                "msg"   => $e->getMessage()
            ]);
        }
        return $content;
    }
    /*
        API : 上传内容
        url : /post/api/post/content
    */
    public function post_content($pid) {
        $V = new \app\validate\post\GetValidate();
        if(!$V->scene("info")->check([
            "pid"   => $pid
        ])) {
            return json(["msg" => $V->getError()]);
        }
        try {
            if ($pid == 0) {
                // 更新
                $this->postLogic->updateContent($pid,input("post.content"));
            } else {
                // 上传
                $this->postLogic->createContent($pid,input("post.content"));
            }
        } catch(Exception $e) {
            return json([
                "code"  => $e->getCode(),
                "msg"   => $e->getMessage()
            ]);
        }
        return json([
            "code"  => 0,
            "msg"   => "更新成功"
        ]);
    }

    /*
        API : 上传文件
        url : /post/api/upload
    */
    public function upload() {
        $files = request()->file("file");
        $savename = [];
        foreach($files as $file) {
            $s = \think\facade\Filesystem::disk('public')->putFile( 'post', $file);
            $savename[$file->getOriginalName()] = "/storage//".$s;
        }
        $return = [
            "code"  => 0,
            "msg"   => "上传成功",
            "data"  => [
                "succMap"   => $savename
            ]
            ];
            return json($return);
    }

    /*
        API : 获取列表信息
        url : /post/api/get/list

        type 为 获取的类型
    */
    public function get_list() {
        $type = input("get.type");
        if ($type == "all")
            return json(PostModel::order('pid', 'asc')->column(["pid","title","update_time","uid","type"]));
        else
            return json(PostModel::where("type",$type)->order('pid', 'asc')->column(["pid","title","update_time","uid","type"]));
    }
    // 获取类型列表
    public function get_type() {
        return json(TypeModel::select());
    }
}